﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using CommonLib.BLL;

public partial class CRM_Store_ERP_SalePayManager : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState_OrderSre = "InDate desc";
            AddHandlersToControls();
            gridPanel_listdata.Reload();
            StoreBind();
        }
    }

    private void StoreBind()
    {
        DataTable dt = CRMMis.BLL.ERP_Customer.GetList("1=1").Tables[0];
        Store_Customer.DataSource = dt;
        Store_Customer.DataBind();

        DataTable dtu = SYS_USER.GetList("IS_MANAGER=1").Tables[0];
        Store_USER.DataSource = dtu;
        Store_USER.DataBind();
    }

    private void AddHandlersToControls()
    {
        string Url = G_Url_allstring + "/CRM_Store/ERP_SalePayEdit.aspx";

        string tabid = base.G_FrameTabid;
        string handler = "popfortab(gridPanel_listdata, '{0}', '{1}', '{2}', {3}, '{4}', '{5}', '{6}', {7});";
        extBtn_Add.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "Add", 0, "", tabid, "结算单（详细）", 1);
        extBtn_Edit.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "PayId", 0, "", tabid, "结算单（详细）", 2);
        gridPanel_listdata.Listeners.RowDblClick.Handler = string.Format(handler, Url, "ObjId", "PayId", "rowIndex", "", tabid, "结算单（详细）", 3);
    }

    string _QueryString;
    private string GetQryString()
    {
        string result = " 1=1 ";

        if (exttxtf_PayNo.Text.Trim() != "")
            result += " and PayNo like'%" + FilterSpecChar(exttxtf_PayNo.Text.Trim()) + "%'";
        if (extcomb_CustomId.SelectedItem.Text.Trim() != "")
            result += " and Customer ='" + FilterSpecChar(extcomb_CustomId.SelectedItem.Value.Trim()) + "'";
        if (extcomb_USER.SelectedItem.Text.Trim() != "")
            result += " and Saler ='" + FilterSpecChar(extcomb_USER.SelectedItem.Value.Trim()) + "'";
        if (extcomb_State.SelectedItem.Text != "" && extcomb_State.SelectedItem.Value != "-1")
            result += " and State ='" + FilterSpecChar(extcomb_State.SelectedItem.Value.Trim()) + "'";
        if (extdate_start.SelectedValue != null)
            result += " and InDate >='" + extdate_start.Value.ToString() + "'";
        if (extdate_end.SelectedValue != null)
            result += " and InDate <='" + extdate_end.Value.ToString() + "'";
        return result;
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        _QueryString = GetQryString();
        int count = CRMMis.BLL.ERP_SalePay.GetRecorderCount(_QueryString);
        BindGridPSData(e, ViewState_OrderSre, PagingToolbar1, Store1, count);
    }

    public override DataTable GetPSData(int PageSize, int CurPage, string OrderStr)
    {
        ViewState_OrderSre = OrderStr;
        DataTable dt = CRMMis.BLL.ERP_SalePay.GetPageList(PageSize, CurPage, _QueryString, OrderStr).Tables[0];
        return dt;
    }

    [DirectMethod]
    public void DeleteModel(int PayId)
    {
        CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
        try
        {
            var model = CRMMis.BLL.ERP_SalePay.GetModel(PayId);
            //判断是否有审核员角色
            if (BS_LoginRoles.Contains("02"))
            {
                if (model.state == 2)
                {
                    X.Msg.Alert("系统提示", "已审核的数据不能删除！").Show();
                    return;
                }
            }
            else    //普通用户
            {
                if (model.state > 0)
                {
                    X.Msg.Alert("系统提示", "已提交的数据不能删除！").Show();
                    return;
                }
            }
            myTrans.BeginTransaction();
            CRMMis.BLL.ERP_SalePay_Detail.DeleteFK(myTrans, "PayId = " + PayId);
            CRMMis.BLL.ERP_SalePay.Delete(myTrans, PayId);
            myTrans.CommitTran();
            X.Msg.Alert("系统提示", "删除成功！").Show();
            gridPanel_listdata.Reload();

        }
        catch (Exception ex)
        {
            myTrans.RollBackTran();
            X.Msg.Alert("系统提示", "删除失败！</br>" + ex.Message).Show();
        }
    }
}